package cn.openea.perpartner.meily.web.controller;


import cn.openea.perpartner.common.core.config.SystemConfig;
import cn.openea.perpartner.common.core.utils.StringUtils;
import cn.openea.perpartner.common.core.utils.file.FileUploadUtils;
import cn.openea.perpartner.common.core.utils.file.FileUtils;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.meily.util.CosUpload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 通用请求处理
 */
@Slf4j
@Api(tags = "文件上传")
@Controller
@RequestMapping("/meily")
public class UploadController {

    @Autowired
    private CosUpload cosUpload;
    private static final String FILE_DELIMETER = ",";


    @ApiOperation(value = "文件上传")
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = SystemConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            File localFile = new File(filePath + "/" + fileName);
            String url = cosUpload.uploadFile(localFile, false);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "多文件上传")
    @PostMapping("/uploads")
    @ResponseBody
    public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception {
        try {
            // 上传文件路径
            String filePath = SystemConfig.getUploadPath();
            List<String> urls = new ArrayList<String>();
            for (MultipartFile file : files) {
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                File localFile = new File(filePath + "/" + fileName);
                String url = cosUpload.uploadFile(localFile, false);
                urls.add(url);
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    @ApiOperation(value = "清空文件夹")
    @PostMapping("/file/clear")
    @ResponseBody
    public AjaxResult clear() throws Exception {
        String filePath = SystemConfig.getUploadPath();
        FileUtils.emptyDir(filePath);
        return AjaxResult.success();
    }
}
